/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MetricsRetriever;

import General.*;
import java.io.File;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Manuel
 */
public class RetrievalManager {

    private DataManager datamgr;
    private String[] fileNames;
    private ArrayList<Thread> crawlingThreads;

    public static int noSuccess = 0;
    public static int noFailedContext = 0;
    public static int noFailedByte = 0;
    public static int noFailedCode = 0;

    public RetrievalManager(String[] fnames){
        crawlingThreads = new ArrayList<Thread>();
        fileNames = fnames;
        datamgr = new DataManager();
    }

    public void processLinks(){
        
        //eliminate old output files
        Utilities.writeToFile(Utilities.getCurrentTime() + ";", Globals.projectFile, false);
        FileManager.removeDir(new File(Globals.DATA_DIRECTORY));
        //first parse the files to get a set of links
        int fileNo = fileNames.length;
        String currentFile;
        String content="";
        Project currentProject;
        Retriever retriever;

        for(int i = 0; i<fileNo; i++){
            currentFile = fileNames[i];
            content = Utilities.readFromFile(currentFile);
            int index = i +1;
            this.parseLinks(content,index);
        }

        //start several threads each taking then the links to be parsed until they finish
        for(int i = 0;i<Globals.threadNumber;i++){
            GoogleRetriever r1 = new GoogleRetriever(this.datamgr);
            SourceForgeRetriever r2 = new SourceForgeRetriever(this.datamgr);
            r1.start();
            r2.start();
            crawlingThreads.add(r1);
            crawlingThreads.add(r2);
        }
        for(int i = 0;i<crawlingThreads.size();i++){
            try {
                crawlingThreads.get(i).join();
            } catch (InterruptedException ex) {
                Logger.getLogger(RetrievalManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        System.out.println("\nSUCCESS: "+RetrievalManager.noSuccess+"\nCONTEXTFAIL: "+RetrievalManager.noFailedContext+"\nBYTEFAIL: "+RetrievalManager.noFailedByte+"\nCODEFAIL: "+RetrievalManager.noFailedCode);
    }

    public void parseLinks(String content, int offset) {
        //first eliminate the date
        int indexOfLim = content.indexOf(";");
        String currentLink;

        //now extract every link and add it into the datamanager
        int insertIndex = 0;
        while(indexOfLim != -1){
            currentLink = content.substring(0, indexOfLim);
            datamgr.addNewLink(insertIndex,currentLink);
            content = content.substring(indexOfLim+1);
            indexOfLim = content.indexOf(";");
            insertIndex += offset;
        }
    }




}
